import time
import unittest
import datetime
from common.config_parse import load_yaml
from biz.flow.user import UserRequest
from biz.flow.per_report import PerReportRequest
from biz.flow.ent_report import EntReportRequest
from biz.flow.per_own_report import PerOwnReportRequest
from biz.flow.ent_own_report import EntOwnReportRequest

from common.log import Log
class BaseCase(unittest.TestCase):
    # data_file_name = None
    data_file_name = 'login_data.yaml'
    @property
    def file_data(self):
        data = load_yaml(self.data_file_name)
        return data

    def get_token_admin(self):
        '''获取超级管理员用户cookie'''
        username = self.file_data.get('admin')
        password = self.file_data.get('pwd')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        cookie = UserRequest.login_cookie(username, password,phonecode,captcha,mode,needPWD)
        Log.info('cookie:%s' % cookie)
        return cookie

    def get_token_first(self):
        '''获取第一个用户test的cookie'''
        username = self.file_data.get('name')
        password = self.file_data.get('pwd')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        cookie = UserRequest.login_cookie(username, password, phonecode, captcha, mode, needPWD)
        Log.info('cookie:%s' % cookie)
        return cookie

    def get_token(self):
        '''获取cookie'''
        username = self.file_data.get('username')
        password = self.file_data.get('password')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        cookie = UserRequest.login_cookie(username, password,phonecode,captcha,mode,needPWD)
        Log.info('cookie:%s' % cookie)
        return cookie

    def get_token_asyn(self):
        '''异步审批用户登录'''
        username_asyn = self.file_data.get('username_asyn')
        password_asyn = self.file_data.get('password_asyn')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        cookie = UserRequest.login_cookie(username_asyn, password_asyn,phonecode,captcha,mode,needPWD)
        Log.info('cookie:%s' % cookie)
        return cookie

    def get_token_rule(self):
        '''异常查询阻断用户登录'''
        username = self.file_data.get('username_rule')
        password = self.file_data.get('password_rule')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        cookie = UserRequest.login_cookie(username, password,phonecode,captcha,mode,needPWD)
        Log.info('cookie:%s' % cookie)
        return cookie

    def per_archive_create(self):
        '''添加授权档案'''
        archiveType = self.file_data.get('archiveType')
        Num = self.file_data.get('Num')
        clientName = self.file_data.get('clientName')
        cretType = self.file_data.get('cretType')
        cretNo = self.file_data.get('cretNo')
        startDate = datetime.date.today()
        expireDate = datetime.date.today()
        note2 = self.file_data.get('note')
        paperfileid = self.file_data.get('paperfileid')
        filedescribe = self.file_data.get('filedescribe')
        ret = PerReportRequest.archive_create(archiveType, Num, clientName, cretType, cretNo, startDate, expireDate,
                                              note2, paperfileid, filedescribe, BaseCase().get_token())
        Log.info(ret)
        self.assertEqual('00000000', ret.code)
        return ret.msg

    def ent_archive_create(self):
        '''新建授权信息'''
        archiveType = self.file_data.get('archiveType_ent')
        Num = self.file_data.get('Num_ent')
        enterpriseName = self.file_data.get('enterpriseName')
        signCode = self.file_data.get('signCode')
        orgInstCode = self.file_data.get('orgInstCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        startDate = datetime.date.today()
        expireDate = datetime.date.today()
        note = self.file_data.get('note_ent')
        paperFileId = self.file_data.get('paperFileId')
        fileDescribe = self.file_data.get('fileDescribe')
        ret = EntReportRequest.ent_archive_create(archiveType,Num,enterpriseName,signCode,orgInstCode,
                           uniformSocialCredCode,startDate,expireDate,note,
                           paperFileId,fileDescribe,BaseCase().get_token())
        Log.info(ret)
        self.assertIsNotNone(ret.msg)
        return ret.msg

    def per_get_recordid(self):
        '''检索-获取报告的recordid'''
        curPage = self.file_data.get('curPage')
        maxSize = self.file_data.get('maxSize')
        search_EQ_customerName = self.file_data.get('search_EQ_customerName')
        search_EQ_certType = self.file_data.get('search_EQ_certType')
        search_EQ_certNo = self.file_data.get('search_EQ_certNo')
        search_EQ_qryReason = self.file_data.get('search_EQ_qryReason')
        search_EQ_queryType = self.file_data.get('search_EQ_queryType')
        search_EQ_source = self.file_data.get('search_EQ_source')
        search_EQ_status = self.file_data.get('search_EQ_status')
        search_EQ_queryFormat = '00'
        temp = '%s - %s'%(time.strftime('%Y-%m-%d',time.localtime()),time.strftime('%Y-%m-%d',time.localtime()))
        search_GTE_LTE_queryTime_DATE_RANGE = temp
        search_EQ_batchFlag = self.file_data.get('search_EQ_batchFlag')
        ret = PerOwnReportRequest.get_page(curPage,
            maxSize,
            search_EQ_customerName,
            search_EQ_certType,
            search_EQ_certNo,
            search_EQ_qryReason,
            search_EQ_queryType,
            search_EQ_source,
            search_EQ_status,
            search_EQ_queryFormat,
            search_GTE_LTE_queryTime_DATE_RANGE,
            search_EQ_batchFlag,BaseCase().get_token())
        Log.info(ret)
        self.assertIsNotNone(ret.list)
        return ret.list[0].id

    def ent_get_recordid(self):
        '''检索-获取报告的recordid'''
        curPage = self.file_data.get('curPage')
        maxSize = self.file_data.get('maxSize')
        search_EQ_enterpriseName = self.file_data.get('search_EQ_enterpriseName')
        search_EQ_signCode = self.file_data.get('search_EQ_signCode')
        search_EQ_uniformSocialCredCode = self.file_data.get('search_EQ_uniformSocialCredCode')
        search_EQ_orgInstCode = self.file_data.get('search_EQ_orgInstCode')
        search_EQ_qryReason = self.file_data.get('search_EQ_qryReason_ent')
        search_EQ_queryType = self.file_data.get('search_EQ_queryType')
        search_EQ_source = self.file_data.get('search_EQ_source')
        search_EQ_status = self.file_data.get('search_EQ_status')
        search_EQ_queryFormat = '00'
        temp = '%s - %s' % (time.strftime('%Y-%m-%d', time.localtime()), time.strftime('%Y-%m-%d', time.localtime()))
        search_GTE_LTE_queryTime_DATE_RANGE = temp
        ret = EntOwnReportRequest.get_page(curPage,
                                           maxSize,
                                           search_EQ_enterpriseName,
                                           search_EQ_signCode,
                                           search_EQ_uniformSocialCredCode,
                                           search_EQ_orgInstCode,
                                           search_EQ_qryReason,
                                           search_EQ_queryType,
                                           search_EQ_source,
                                           search_EQ_status,
                                           search_EQ_queryFormat,
                                           search_GTE_LTE_queryTime_DATE_RANGE,
                                           BaseCase().get_token())
        Log.info(ret)
        self.assertIsNotNone(ret.list)
        return ret.list[0].id

    def per_show_report(self):
        '''查询报告--进补录'''
        clientName = self.file_data.get('clientName_rule')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note_rule')
        queryFormat = str.zfill('0',2)
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        checkId = ''
        archiveId = ''
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = PerReportRequest.per_show_report(clientName, certType, certNo, assocbsnssData, note, queryFormat,
                                               resultType,
                                               qryReason, queryControlWarningType, serialNumber, archiveId,
                                               autharchiveId, checkId,
                                               rekUser, checkWay, BaseCase().get_token_rule())
        Log.info(ret)
        return ret


    def ent_show_report(self):
        '''人行查报告'''
        enterpriseName = self.file_data.get('enterpriseName_rule')
        signCode = self.file_data.get('signCode')
        uniformSocialCredCode = self.file_data.get('uniformSocialCredCode')
        orgInstCode = self.file_data.get('orgInstCode')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note_rule')
        queryFormat = '00'
        resultType = self.file_data.get('resultType_ent')
        qryReason = self.file_data.get('qryReason_ent')
        queryControlWarningType = self.file_data.get('queryControlWarningType_ent')
        serialNumber = self.file_data.get('serialNumber_ent')
        checkId = ''
        archiveId = ''
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = EntReportRequest.ent_show_report(enterpriseName,
                                                signCode,
                                                uniformSocialCredCode,
                                                orgInstCode,
                                                assocbsnssData,
                                                note,
                                                queryFormat,
                                                resultType,
                                                qryReason,
                                                queryControlWarningType,
                                                serialNumber,
                                                archiveId,
                                                autharchiveId,
                                                checkId,
                                                rekUser,
                                                checkWay,BaseCase().get_token_rule())
        Log.info(ret)
        return ret


if __name__ == "__main__":
    a = BaseCase()
    b = a.get_token()
    print(b)
